package com.uw.uwconfig.demo;

import com.uw.uwconfig.client.annotation.EnableUwConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@Slf4j
@SpringBootApplication
@EnableConfigurationProperties(value = {UWDemoProperty.class})
@EnableUwConfig
@RestController
public class UwConfigDemoApplication implements ApplicationRunner {

    @Value("${uw.a}")
    private String a;
    @Value("${uw.b}")
    private String b;

    @Autowired
    Environment environment;
    @Autowired
    UWDemoProperty uwDemoProperty;

    public static void main(String[] args) {
        SpringApplication.run(UwConfigDemoApplication.class, args);
    }


    @GetMapping("/demo")
    public String demo() {
        return "uw.a = " + a + "\n"
                + "uw.b = " + b + "\n"
                + "uwDemoProperty.a = " + uwDemoProperty.getA() + "\n"
                + "uwDemoProperty.b = " + uwDemoProperty.getB() + "\n";
    }


    @Override
    public void run(ApplicationArguments args) {
        log.info(" ===> active:{}", Arrays.toString(environment.getActiveProfiles()));

        log.info("uw.a value:{}", a);
        log.info("uw.b value:{}", b);
        log.info("uwDemoProperty.a value:{}", uwDemoProperty.getA());
        log.info("uwDemoProperty.b value:{}", uwDemoProperty.getB());
    }
}
